﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ShowProcess : MonoBehaviour {
	public GameObject pointPrefab;

	public Text aText;
	public Text bText;
	public Text x1Text;
	public Text y1Text;

	public Text outputText;

	public float pointsSpeed;

	public InputField aInputField;
	public InputField bInputField;
	public InputField eInputField;
	public Toggle increaseToggle;
	public float a;
	public float b;
	public float e;
	public float x1;
	public float y1;

	private GameObject aPoint;
	private GameObject bPoint;
	private GameObject x1Point;
	private GameObject y1Point;

	// Start is called before the first frame update
	void Start() {
		//放弃路径加载
		//pointPrefab = Resources.Load<GameObject>("Prefabs/point");
		//if (pointPrefab == null) {
		//	Debug.LogError("找不到预制体point!");
		//}
		InitializePoints();
		ResetState();
	}

	// Update is called once per frame
	void Update() {

	}

	public void ResetState() {
		GetInputField();
		x1 = (a + b) * 0.5f;
		y1 = F(x1);
		MovePoint();
		UpdateTexts();
	}

	public void OneStep() {
		GetInputField();
		CalculatePosition();
		MovePoint();
		UpdateTexts();
		SetInputField();
	}

	void SetInputField() {
		aInputField.text = a.ToString();
		bInputField.text = b.ToString();
		eInputField.text = e.ToString();
	}
	void GetInputField() {
		a = float.Parse(aInputField.text);
		b = float.Parse(bInputField.text);
		e = float.Parse(eInputField.text);
	}

	void UpdateTexts() {
		aText.text = "a: " + a;
		bText.text = "b: " + b;
		x1Text.text = "x1: " + x1;
		y1Text.text = "y1: " + y1;
	}

	void MovePoint() {
		aPoint.GetComponent<Point>().ChangeAimPos(a, 0);
		bPoint.GetComponent<Point>().ChangeAimPos(b, 0);
		x1Point.GetComponent<Point>().ChangeAimPos(x1, 0);
		y1Point.GetComponent<Point>().ChangeAimPos(x1, y1);
	}

	void CalculatePosition() {

		if (b - a <= e) {//找到解
			outputText.text = x1.ToString();
		}
		else {
			outputText.text = null;
		}
		if (!increaseToggle.isOn) {
			//2Sinx单调递减区域
			if (y1 > x1) {
				//中值偏大，解在右边
				a = x1;
			}
			else if (y1 < x1) {
				b = x1;
			}
			else {
				outputText.text = x1.ToString();
			}
		}
		else {
			//2Sinx单调递增区域
			if (y1 < x1) {
				//中值偏小 解在右边
				a = x1;
			}
			else if (y1 > x1) {
				b = x1;
			}
			else {
				outputText.text = x1.ToString();
			}
		}
		x1 = (a + b) * 0.5f;
		y1 = F(x1);
	}
	void InitializePoints() {
		aPoint = Instantiate(pointPrefab, Vector3.zero, Quaternion.identity);
		bPoint = Instantiate(pointPrefab, Vector3.zero, Quaternion.identity);
		x1Point = Instantiate(pointPrefab, Vector3.zero, Quaternion.identity);
		y1Point = Instantiate(pointPrefab, Vector3.zero, Quaternion.identity);

		aPoint.GetComponent<Point>().color = Color.green;
		bPoint.GetComponent<Point>().color = Color.green;
		x1Point.GetComponent<Point>().color = Color.red;
		y1Point.GetComponent<Point>().color = Color.red;
	}

	float F(float x) {
		return 2 * Mathf.Sin(x);
	}

	public void SetSection1() {
		a = -3.14f;
		b = -1.57f;
		increaseToggle.isOn = false;
		SetInputField();
		ResetState();
	}
	public void SetSection2() {
		a = -1.57f;
		b = 1.57f;
		increaseToggle.isOn = true;
		SetInputField();
		ResetState();
	}
	public void SetSection3() {
		a = 1.57f;
		b = 3.14f;
		increaseToggle.isOn = false;
		SetInputField();
		ResetState();
	}


}
